package uit.edu.vn.Utility;

import java.util.ArrayList;
import java.util.HashMap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.util.Log;

public class WebService {

	public enum Result {
		SUCCESSED, FAILED, EXCEPTION;
	}

	
	// OK
	public static String checkLogin(String username, String password)
			throws Exception {
		String SOAP_ACTION = "http://10.0.2.2:8080/ServerWeb/services/Service/CheckLogin";
		String METHOD_NAME = "CheckLogin";
		String NAMESPACE = "http://vn.edu.uit";
		String URL = "http://10.0.2.2:8080/ServerWeb/services/Service?wsdl";		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("username", username);
		request.addProperty("password", password);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
		Log.i("CheckLogin:", "496 - Calling function...");
		androidHttpTransport.call(SOAP_ACTION, envelope);
		Log.i("CheckLogin:", "496 - passed");		
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		Log.i("CheckLogin:", "498 - get response");
		String myResult = result.toString();
		return myResult;			
	}

	// OK
	public static String createAccount(String username, String password,
			String email) throws Exception {
		String SOAP_ACTION = "http://10.0.2.2:8080/ServerWeb/services/Service/CreateAccount";
		String METHOD_NAME = "CreateAccount";
		String NAMESPACE = "http://vn.edu.uit";
		String URL = "http://10.0.2.2:8080/ServerWeb/services/Service?wsdl";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		request.addProperty("username", username);
		request.addProperty("password", password);
		request.addProperty("email", email);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
				URL);

		androidHttpTransport.call(SOAP_ACTION, envelope);
		SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
		String myResult = result.toString();
		return myResult;
	}


}

